import { abilityAccessCtrl, bundleManager, Permissions } from '@kit.AbilityKit';

class CPermissionUtil {
  async ensure(permission: Permissions) {
    const atMan = abilityAccessCtrl.createAtManager();
    const bInfo = bundleManager.getBundleInfoForSelfSync(bundleManager.BundleFlag.GET_BUNDLE_INFO_WITH_APPLICATION);
    const permissionStatus = atMan.verifyAccessTokenSync(bInfo.appInfo.accessTokenId, permission);
    if (permissionStatus != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) {
      await atMan.grantUserGrantedPermission(bInfo.appInfo.accessTokenId, permission, 2)
    }
  }
}

export const PermissionUtil = new CPermissionUtil;